package com.zhangxuehai.pdfviewer;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.zhangxuehai.android.pdfviewer.AnnotionListener;
import com.zhangxuehai.android.pdfviewer.PDFView;
import com.zhangxuehai.android.pdfviewer.annotation.AnnotationBean;
import com.zhangxuehai.android.pdfviewer.annotation.BaseAnnotation;
import com.zhangxuehai.android.pdfviewer.listener.OnPageChangeListener;
import com.zhangxuehai.android.pdfviewer.util.FitPolicy;
import com.google.gson.reflect.TypeToken;
import com.zhangxuehai.pdfviewer.ui.PdfControlView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * pdfviewer 使用demo页码
 * @author zhangxuehai*/
public class MainActivity extends AppCompatActivity {
    private static final String PDF_NAME = "file.pdf";
    private static final String PDF_PASSWORD = "123456";

    @BindView(R.id.PDFView)
    PDFView pdfView;
    @BindView(R.id.pdfControlView)
    PdfControlView pdfControlView;

    PDFView.Configurator configurator;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        openPdf(PDF_NAME,PDF_PASSWORD);
        //load saved Annotations
        String saveJson =  CacheDiskUtils.getInstance().getString(PDF_NAME,null);
        if(ObjectUtils.isNotEmpty(saveJson)){
            List<AnnotationBean> annotations = GsonUtils.fromJson(saveJson,new TypeToken<List<AnnotationBean>>(){}.getType());
            pdfView.addAnnotations(annotations);
        }
        pdfView.setMinZoom(1F);
        pdfView.setMidZoom(2F);
        pdfView.setMaxZoom(3F);
        pdfView.setAnnotionListener(annotionListener);
        pdfControlView.bindPdfView(pdfView,configurator);
        pdfControlView.setCall(controlCall);
    }

    /**打开pdf文件
     * @param password pdf文件的密码*/
    void openPdf(@NonNull String assetFileName, @Nullable String password){
        configurator = pdfView.fromAsset(assetFileName);
        configurator.password(password);
        //滚动方向
        configurator.swipeHorizontal(false);
        //滚动完成后自动滚动到当前页适当的显示位置
        configurator.pageSnap(false);
        //护眼模式
        //configurator.eyeMode(true);
        //设置2pdf页面间距
        configurator.spacing(0);
        //根据适配方向,自动居中且加上间距，同时给给offset加上间距
        //configurator.autoSpacing(true);
        //滑动页面支持
        configurator.pageFling(true);
        //适配策略
        configurator.pageFitPolicy(FitPolicy.WIDTH);
        configurator.enableAnnotationRendering(false);
        configurator.onPageChange(pageChangeListener);
        configurator.linkHandler(null);
        configurator.load();
    }

    OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
        @Override
        public void onPageChanged(int curPage,int allPage) {
            pdfControlView.onPage(curPage+1,allPage);
        }
    };
    private AnnotionListener annotionListener = new AnnotionListener() {
        @Override
        public void onAnnotionAdd(BaseAnnotation baseAnnotation) {
            //save Annotion
        }
        @Override
        public void onAnnotionRemove(BaseAnnotation baseAnnotation) {
            //delete Annotion
        }

        @Override
        public void onAnnotionPageRemove(int page) {
            //delete Annotion of page
        }

        @Override
        public void onAnnotionAllRemove() {
            //delete all Annotion
        }
    };

    PdfControlView.Call controlCall = new PdfControlView.Call() {
        @Override
        public void onForward() {

        }

        @Override
        public void onCollect(boolean collect) {
            pdfControlView.setCollect(collect);
        }

        @Override
        public void onNote() {

        }
    };

    @Override
    protected void onDestroy() {
        if (pdfView!=null) {
            CacheDiskUtils.getInstance().put(PDF_NAME, GsonUtils.toJson(pdfView.getAllAnnotation()));
            pdfView.recycle();
        }
        super.onDestroy();
    }


}
